package com.freshfresh.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.StrikethroughSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.freshfresh.activity.R;
import com.freshfresh.utils.ImageLoadOptions;
import com.freshfresh.utils.RequestManager;
import com.freshfresh.utils.StringUtil;
import com.freshfresh.utils.UrlConstants;
import com.freshfresh.utils.Utils;
import com.nostra13.universalimageloader.core.ImageLoader;

public class MyFavoriteAdapter extends BaseAdapter {

	List<Map<String, String>> apk_list;
	LayoutInflater inflater;
	protected OnCustomListener listener;
	private Context context;
	String userid ;
	String token;
	Map<String, String> entity;

	
	public MyFavoriteAdapter(Context context, List<Map<String,String>> apk_list, String userid, String token) {
		this.context = context;
		this.userid = userid ;
		this.token = token ;
		this.apk_list = apk_list;
		this.inflater = LayoutInflater.from(context);
		
	}

	@Override
	public int getCount() {
		return apk_list.size();
	}

	@Override
	public Object getItem(int position) {
		return apk_list.get(position);
		
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	

	/**
	 * listView Item设置
	 */
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		 entity = apk_list.get(position);

		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.ac_my_favorite_list_item,
					null);
			holder.root = (ViewGroup) convertView.findViewById(R.id.root);
			holder.img_icon = (ImageView) convertView
					.findViewById(R.id.img_icon);
			holder.tv_guoqi = (ImageView) convertView
					.findViewById(R.id.tv_guoqi);
			holder.tv_guige = (TextView) convertView
					.findViewById(R.id.tv_guige);
			holder.tv_title = (TextView) convertView
					.findViewById(R.id.tv_title);
			holder.tv_price = (TextView) convertView
					.findViewById(R.id.tv_price);
			holder.tv_price_orignal = (TextView) convertView
					.findViewById(R.id.tv_price_orignal);
			holder.tv_buy = (TextView) convertView.findViewById(R.id.tv_buy);
			holder.tv_buy = (TextView) convertView.findViewById(R.id.tv_buy);
			holder.btn_cancellation = (Button) convertView
					.findViewById(R.id.btn_cancellation);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.btn_cancellation.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
					System.out.println("你点击了取消收藏！！");
					// shoppingposition = position ;
					 deleteWishListProduct(position);//删除收藏
					 System.out.println("删除成功？！");
			}
		});
		ImageLoader.getInstance().displayImage(
				entity.get("image").toString(), holder.img_icon,
				ImageLoadOptions.getOptions(0));

		ImageLoader.getInstance().displayImage(
				entity.get("national_flag").toString(), holder.tv_guoqi,
				ImageLoadOptions.getOptions(0));

		holder.tv_title.setText(entity.get("name").toString());
		holder.tv_price.setText(StringUtil.formatStringToWish(null,
				entity.get("price").toString()));

		// todo
		holder.tv_guige.setText(entity.get("standard2").toString());
		// todo 缺少特价

		holder.tv_price.setText(StringUtil.formatStringToWish("￥0.00", entity
				.get("special_price").toString()));
		String price = StringUtil.formatStringToWish("￥0.00",
				entity.get("price").toString());

//		String stand = "";
//		if ("simple".equals(entity.get("type").toString())) {
//			stand = "规格:" + entity.get("standard2").toString();
//			((View) holder.tv_isInCar.getParent()).setVisibility(View.VISIBLE);
//		} else {
//			if (entity.get("bundle_list").toString() != null
//					&& entity.get("bundle_list").toString().length() != 0) {
//				for (int i = 0; i < entity.get("bundle_list").toString()
//						.length(); i++) {
//					if (i > 0) {
//						// stand += " "
//						// + bean.getBundle_list().get(i).getStandard2();
//						// stand
//						// +=" "+entity.get("bundle_list").toString().getStandard2();
//					} else {
//						// stand += ""
//						// + bean.getBundle_list().get(i).getStandard2();
//					}
//				}
//			}
//			 holder.tv_isInCar.getParent();
//					
//		}
//		holder.tv_guige.setText(stand + "");

		SpannableString msp = new SpannableString(price);
		// 设置字体样式正常，粗体，斜体，粗斜体
		// 设置删除线
		msp.setSpan(new StrikethroughSpan(), 0, price.length(),
				Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		holder.tv_price_orignal.setText(msp);

		// if (!StringUtil.isBlank(entity.get("").toString())
		// && "1".equals(bean.getIn_cart())) {
		// tv_isInCar.setVisibility(View.VISIBLE);
		// } else {
		// tv_isInCar.setVisibility(View.GONE);
		// }

		holder.tv_buy.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
			}
		});
		holder.root.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
			}
		});
		return convertView;
	}
	
	/**
	 * 取消收藏
	 */
	private void deleteWishListProduct(final int position) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("token", token);
		params.put("customerid", userid);
		params.put("wishid", apk_list.get(position).get("wishid"));
		
		System.out.println("url是："+UrlConstants.deleteWishListProduct+"?customerid="+userid+"&token="+token+"&wishid="+entity.get("wishid"));
		executeRequest(new StringRequest(UrlConstants.deleteWishListProduct,
				params, new Response.Listener<String>() {

					@Override
					public void onResponse(String arg0) {
						System.out.println("收藏获取到了~~~"+arg0);
						if (Utils.parseJsonStr(arg0).get("result").toString()
								.equals("1")) {
							// gridAdapter.remove(shoppingposition);
							notifyDataSetChanged();
							Toast.makeText(context, "收藏已取消", 1).show();
//							showToast(context, "收藏已取消");
						} else {
							Toast.makeText(context, "取消收藏失败，请稍候再试",1);
						}
					}

				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError arg0) {
						Toast.makeText(context, "errorcode", 1).show();
					}
				}));

	}

	private void executeRequest(StringRequest request) {
		RequestManager.addRequest(request, this);
		
	}

	class ViewHolder {
		ViewGroup root;
		ImageView img_icon;
		ImageView tv_guoqi;
		TextView tv_guige;
		TextView tv_title;
		TextView tv_price;
		TextView tv_price_orignal;
		TextView tv_buy;
		TextView tv_isInCar;
		Button btn_cancellation;
	}

}
